package com.huishen.edrivenew;

import java.util.Stack;

import com.huishen.edrivenew.ui.LoginActivity;
import com.huishen.edrivenew.ui.SettingActivity;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;

/**
 * 应用程序Activity管理类：管理Activity和应用程序退出
 */

public class AppManager {

	private static Stack<Activity> activityStack;
	private static AppManager instance;
	
	private AppManager() {
		if(activityStack == null) {
			activityStack = new Stack<Activity>();
		}
	}
	
	/**
	 * 获取单例
	 */
	public static AppManager getInstance() {
		if(instance == null) {
			instance = new AppManager();
		}
		return instance;
	}
	
	/**
	 * 添加Activity到栈
	 */
	public void addActivity(Activity activity) {
		if(activityStack == null) {
			activityStack = new Stack<Activity>();
		}
		activityStack.add(activity);
	}
	
	/**
	 * 获取顶层Activity
	 */
	public Activity currentActivity() {
		Activity activity = activityStack.lastElement();
		return activity;
	}
	
	/**
	 * 结束当前Activity（堆栈中最后一个压入的）
	 */
	public void finishActivity() {
		Activity activity = activityStack.lastElement();
		if(activity != null) {
			activity.finish();
			activity = null;
		}
	}
	
	/**
	 * 结束指定的Activity
	 */
	public void finishActivity(Activity activity) {
		if(activity != null) {
			activityStack.remove(activity);
			activity.finish();
			activity = null;
		}
	}
	
	/**
	 * 结束指定类名的Activity
	 */
	public void finishActivity(Class<?> cls) {
		for (Activity activity : activityStack) {
			if(activity.getClass().equals(cls)) {
				this.finishActivity(activity);
			}
		}
	}
	
	/**
	 * 结束所有Activity
	 */
	public void finishAllActivity() {
		int size = activityStack.size();
		for (int i=0; i<size; i++) {
            if (null != activityStack.get(i)) {
            	activityStack.get(i).finish();
            }
	    }
		activityStack.clear();
	}
	
	   public void finishAllActivityNotLogin() {
	        int size = activityStack.size();
	        for (int i=0; i<size; i++) {
	            if (null != activityStack.get(i)) {
	                if(!(activityStack.get(i) instanceof LoginActivity)) {
	                    activityStack.get(i).finish();
	                }
	            }
	        }
	        activityStack.clear();
	    }
	
	
	   /**
	    * 退出 或 320
	    */
       public void logout(Context context) {
           if(context == null) {
               context = AppContext.context();
           }
           
           AppContext.getInstance().saveLogin("0");
           Intent intent = new Intent(context, LoginActivity.class);
           intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
           context.startActivity(intent);
           AppManager.getInstance().finishAllActivityNotLogin();
       }
	   
	
	/**
	 * 退出应用程序
	 */
	public void AppExit(Context context) {
		try {
			this.finishAllActivity();
			ActivityManager activityMgr = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
			activityMgr.restartPackage(context.getPackageName());
//			System.exit(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
    //退出对话框
    public void showExitDialog(final Activity context){
    	AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setMessage("确定退出？");
		builder.setNegativeButton("取消", null);
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				AppManager.this.AppExit(context);
				context.finish();
			}
		});
		AlertDialog alert = builder.create();
		alert.show();
    }
    
    //退出应用程序
    public void showExitDialog2(final Activity context){
    	AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setMessage("确定退出？");
		builder.setNegativeButton("取消", null);
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				AppManager.this.AppExit(context);
				context.finish();
			}
		});
		AlertDialog alert = builder.create();
		alert.show();
    }
	
	
}
